


/**
 * 简单打包解析数据
 */
export  default  class PackData{
    /**
     * 第一个块是用来装载基本信息，
     * 比如，这个模块叫什么，
     * 目录起始块到结束块，（通常目录丢到最后）
     */
    constructor(){
        let buff=new Uint8Array(1250)
        this.list=[ ];
        this.list.push(buff)
        this.byteOffset=0;
        this.length=1;
        this.paths=""

    }

    /**
     * 字符串转 buff
     * 返回每个块1万比特大小的列表；
     * @param data
     * @returns {Array}
     */
    createData(data){
        let length=0;
       let textEncoder= new TextEncoder( );
        let uint8array = textEncoder.encode(data);
        let list=[];
        while (uint8array.byteLength-(1250*length)>0){

            let uint8array1=new Uint8Array(1250);
            uint8array1.set( uint8array.subarray(length*1250, length*1250+1250))
            list.push( uint8array1)
            length++;
        }
        return list;
    }

    /**
     * 添加数据
     * @param data
     * @param path
     */
    push(data,path,type){
        let _this=this;
        let length=0;
        let start=_this.length;
        let buff = new Uint8Array(data);
        //  let buff = new TextEncoder().encode(this.result);
        if (data) {
            while (buff.byteLength - 1250 * length > 0) {
                let uint8array1 = new Uint8Array(1250);
                uint8array1.set(buff.subarray(length * 1250, length * 1250 + 1250))
                _this.list.push(uint8array1)
                length++;
            }
        }
        let end = _this.length + length ;
        _this.length += length;
        _this.paths += path + "," + start + "," + end + "," + type + "/n";



    }

    /**
     * 获取全部数据
     * @returns {uint8array[]|*[]}
     */
    findAllBuff(){
        let pathsBuff= this.createData(this.paths);
        let synopsis="索引,"+this.length+","+(this.length+pathsBuff.length) +",索引";
        for(let i=0;i<pathsBuff.length;i++){
            this.list.push( pathsBuff[i])
        }
        let synopsisBuff= this.createData(synopsis);
        this.list[0]=synopsisBuff[0]
        return this.list;
    }

    /**
     * 回填数据
     * @param data
     */
    setData(data){
        this.length=0;
        this.list=[]
        this.push(data,"")
        let path=this.findPath();
        this.paths=path;
    }

    /**
     * 获取目录字符串
     * @returns {*|number|void}
     */
    findPath(){
        let synopsisBuff   = this.list[0];
       let textDecoder= new TextDecoder();
        let  synopsis=textDecoder.decode(synopsisBuff);
        let listSyn=synopsis.split(",")

        let pathsBuff =this.findData(listSyn[1],listSyn[2])
        let path=""
        for(let i=0;i<pathsBuff.length;i++){
            let textDecoder1= new TextDecoder();
            path+=   textDecoder1.decode(pathsBuff[i]);
        }
        return path;
    }

    /**
     * 获取连续几个块的数据
     * @param start
     * @param end
     * @returns {Array}
     * @private
     */
    findData(start,end){
        let list=[];
        for(let i=start;i<end;i++){
            list.push(this.list[i])
        }
        return list;
    }
    /**
     * 获取连续几个块的的文本
     * @param start
     * @param end
     * @returns {Array}
     * @private
     */
    findText(start,end){
        let pathsBuff =this.findData(start,end)
        let res=""

        for(let i=0;i<pathsBuff.length;i++){
           let text= new TextDecoder();
            res+=  text .decode(pathsBuff[i]);
        }
        return res;
    }

}